package com.ruoyi.generator.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.generator.entity.ImportWaterQuality;
import com.ruoyi.generator.po.Device;
import com.ruoyi.generator.po.ScheduleShift;
import com.ruoyi.generator.service.AppService;
import com.ruoyi.generator.service.InspectionService;
import com.ruoyi.generator.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping("/site")
public class SiteController extends BaseController {

    @Autowired
    private SiteService siteService;


    @Autowired
    private AppService appService;

    @PostMapping("/equipment/addSevice")
    public AjaxResult addSevice(@RequestBody Device device)
    {

         int result = siteService.addSevice(device);
         return  AjaxResult.success(result);
    }
    @GetMapping("equipment/list")
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = siteService.selectDeviceList(device);
        return getDataTable(list);
    }


    private final String BASE_PATH = "E:/fctextjavaworkspace/wushuichang/upload/device/";
    @GetMapping("/image/{imageName}")
    public Resource getImage(@PathVariable String imageName) {

        try {
            Path file = Paths.get(BASE_PATH).resolve(imageName).normalize();
            Resource resource = new UrlResource(file.toUri());
            if (resource.exists() || resource.isReadable()) {
                return resource;
            } else {
                throw new RuntimeException("Could not read the file!");
            }
        } catch (Exception e) {
            throw new RuntimeException("File not found: " + imageName);
        }
    }


    /**
     * 获取水表数据
     *  type  inflow 进水   outflow 出水
     * */
    @GetMapping("/getWaterData/{type}")//获取保养数据    pending未开始  progress进行中    expired 已过期
    public AjaxResult getWaterData(@PathVariable String type) {
        if ("inflow".equals(type)) {
            ImportWaterQuality importWaterQuality = appService.getImportWaterData();
            return AjaxResult.success(importWaterQuality);
        } else if ("outflow".equals(type)) {
            ImportWaterQuality exportWaterQuality = appService.getExportWaterData();
            return AjaxResult.success(exportWaterQuality);
        } else {
            return AjaxResult.error("Invalid type");
        }
    }



    }
